import json
import logging
import pytest
import requests
class Test():
    token = ""
    spid = ""
    def setup_method(self):
        print("执行前用例")
    def teardown_method(self):
        print("执行后用例")
    #登录--正例
    def test_denglu(self):
        try:
            url="http://10.59.9.12:7001/api/public/login"
            doby={"username":"13225405754","password":"ldk123456"}
            heap={"content-type":"application/json"}
            r = requests.post(url=url,json=doby,headers=heap)
            print("登录",r.json())
            Test.token ="Bearer " +r.json()['data']['token']
        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert "登陆成功" == r.text
    # 查看商品详情--正例

    def test_shangpin(self):
        try:
            url = "http://10.59.9.12:7001/api/goods/view?id=123"
            heap = {"token":Test.token}
            r = requests.get(url=url,  headers=heap)
            print("查看商品详情", r.json())

        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert "阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机" == r.text
    # 加入购物车--正例
    def test_jiarugouw(self):
        try:
            url = "http://10.59.9.12:7001/api/cart/add"
            doby={"id":123,"sku_id":"","num":1}
            heap = {"auth-token":Test.token}
            r = requests.post(url=url, json= doby,headers=heap)
            print("加入购物车", r.json())


        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert "商品添加至购物车成功" == r.text

        # 购物车商品详情--正例
    def test_gouwuche(self):
        try:
            url = "http://10.59.9.12:7001/api/cart?page=1"
            heap = {"auth-token": Test.token}
            r = requests.get(url=url,  headers=heap)
            print("购物车页面", r.json())

            Test.spid = r.json()['data']['list'][0]['id']
        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert "阿诗顿厨师机A6家用7升商用小型和面机静音自动多功能鲜奶揉面机" == r.text
        # 提交订单并支付--正例
    def test_zhifu(self):
        try:
            url = "http://10.59.9.12:7001/api/order/create"
            doby={"id":Test.spid,"type":"cart","address_id":83,"store_id":2,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":f"http://10.59.9.12:7001/wap/pages/cart/confirm?id=2111&type=cart"}
            heap = {"auth-token": Test.token}
            r = requests.post(url=url, json=doby, headers=heap)
            print("提交订单并支付", r.json())

        except Exception as e:
            logging.info(e)
            assert r.status_code == 200
            assert "支付成功" == r.json()


